<template>
  <el-dialog :visible="visible" :title="title" @close="close" width="56%" :close-on-click-modal="false">
    <el-form ref="form" :model="menu" label-position="top">
        <el-col :span="24">
            <el-form-item
              label="菜单名称"
              prop="menu_NAME"
              :rules="[{ required: true, message: '请输入菜单名称', trigger: 'blur' }]"
            >
              <el-input v-model="menu.menu_NAME" placeholder="请输入菜单名称" clearable />
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item
              label="菜单地址"
              prop="menu_URL"
              :rules="[{ required: true, message: '请输入菜单地址', trigger: 'blur' }]"
            >
              <el-input v-model="menu.menu_URL" placeholder="请输入菜单地址"  clearable />
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item
              label="菜单类型"
              prop="menu_TYPE"
              :rules="[{ required: true, message: '请选择菜单类型', trigger: 'blur' }]"
            >
              <el-select v-model="menu.menu_TYPE" placeholder="请选择菜单类型" clearable style="width: 100%;">
               <el-option label="系统" :value="'1'" />
                <el-option label="业务" :value="'2'" />
              </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item
              label="菜单ICON"
              prop="menu_ICON"
              :rules="[{ required: true, message: '请输入菜单ICON', trigger: 'blur' }]"
            >
              <el-input v-model="menu.menu_ICON" placeholder="请输入菜单ICON" clearable />
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item
                label="菜单顺序"
                prop="menu_ORDER"
                :rules="[{ required: true, message: '请输入菜单顺序', trigger: 'blur' }]"
                >
               <el-input-number v-model="menu.menu_ORDER" label="请输入菜单顺序"></el-input-number>
            </el-form-item>
        </el-col>
    </el-form>
    <span slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { menuManageService } from '@/services';

export default {
  name: 'menuEdit',
  props: {
    operation: {
      default: 'add',
      validator: (value) => {
        return ['add', 'update'].indexOf(value) !== -1;
      },
    },
    visible: {
      type: Boolean,
      required: true,
    },
    menu: {
      type: Object,
      required: true,
    },
  },
  computed: {
    title() {
      return this.operation === 'add' ? '添加菜单' : '修改菜单';
    },
  },
  methods: {
    close() {
      this.$emit('update:visible', false);
    },
    async confirm() {
      try {
        await this.$refs.form.validate();
       
        if(this.operation === 'add')
        await menuManageService.addMenu(this.menu);
        else
        await menuManageService.editMenu(this.menu);

        this.$emit('confirm');
      } catch (e) {
        console.error(e);
      }
    },
    resetForm () {
      if(this.$refs.form!=null){
          this.$refs.form.resetFields();
      }
    }
  },
};
</script>